<template>
  <!-- 刷卡 -->
  <el-dialog title="登记/激活" :visible="value" width="400px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <!-- <div>
      <el-radio-group v-model="type">
        <el-radio :label="1">扫描二维码</el-radio>
        <el-radio :label="2">刷就诊卡</el-radio>
      </el-radio-group>
    </div>
    <br> -->
    <el-form @submit.native.prevent="onSubmit">
      <el-input :disabled="disabled" @focus="onFocus" v-model="inputValue" class="dialog-input-box" ref="input1" placeholder="支持二维码和刷卡" type="password"></el-input>
    </el-form>
    <iframe ref="iframe1" frameborder="0" height="0px" width="0px" src="/print/bf_patientregister.html"></iframe>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "dateDay", "curItem", "mode"],
  data() {
    return {
      inputValue: "",
      disabled: false
    };
  },
  methods: {
    //北妇逻辑
    bfLogic() {
      let th = this;
      let str = th.inputValue;
      let isExist = str.includes("?");
      //提交逻辑
      let j = {
        hospitalCard: ""
      };
      if (isExist) {
        //刷卡
        str = "0000" + str.substring(1, 9);
        j.hospitalCard = str;
      } else {
        //二维码
        str = str.replace(/'/g, '"');
      }
      //登记到随机号源
      let fn = async function() {
        j.type = 2;
        j.dateDay = th.dateDay;
        j.doctorId = th.curItem.id;
        th.disabled = true;
        if (!isExist) {
          let bf = {
            visitId: JSON.parse(str).visitId
          };
          let bfRes = await th.$api.usUserVisitselectVisitInfoNew(bf);
          j.hospitalCard = bfRes.visitUserNewInfo.hisCode;
        }
        let res1 = await th.$api.registeraddOrderSingle(j);
        if (res1.executed) {
          th.$message.success("提交成功！");
          th.$emit("success");
          th.onClose();
          th.onPrint(res1.dataBody);
        } else {
          th.$message.error(res1.message);
        }
        th.disabled = false;
      };
      //登记到指定号源
      let fn2 = async function() {
        j.sourceId = th.curItem;
        th.disabled = true;
        if (!isExist) {
          let bf = {
            visitId: JSON.parse(str).visitId
          };
          let bfRes = await th.$api.usUserVisitselectVisitInfoNew(bf);
          j.hospitalCard = bfRes.visitUserNewInfo.hisCode;
        }
        let res1 = await th.$api.addOrderBySourceId(j);
        if (res1.executed) {
          th.$message.success("提交成功！");
          th.$emit("success");
          th.onClose();
          th.onPrint(res1.dataBody);
        } else {
          th.$message.error(res1.message);
        }
        th.disabled = false;
      };
      if (th.mode == 1) {
        fn2();
      } else {
        fn();
      }
    },
    //其他逻辑
    otherLogic() {
      let th = this;
      let j = {
        hospitalCard: th.inputValue
      };
      //登记到随机号源
      let fn = async function() {
        j.type = 2;
        j.dateDay = th.dateDay;
        j.doctorId = th.curItem.id;
        th.disabled = true;
        let res1 = await th.$api.addOrderSingleOnline(j);
        if (res1.executed) {
          th.$message.success("提交成功！");
          th.$emit("success");
          th.onClose();
          th.onPrint(res1.dataBody);
        } else {
          th.$message.error(res1.message);
        }
        th.disabled = false;
      };
      //登记到指定号源
      let fn2 = async function() {
        j.sourceId = th.curItem;
        th.disabled = true;
        let res1 = await th.$api.addOrderBySourceIdOnline(j);
        if (res1.executed) {
          th.$message.success("提交成功！");
          th.$emit("success");
          th.onClose();
          th.onPrint(res1.dataBody);
        } else {
          th.$message.error(res1.message);
        }
        th.disabled = false;
      };
      if (th.mode == 1) {
        fn2();
      } else {
        fn();
      }
    },
    onSubmit() {
      let th = this;
      switch (th.adminUser.hpId) {
        case "35":
          //北妇医院
          th.bfLogic();
          break;
        default:
          //其他医院（国信）
          th.otherLogic();
          break;
      }
      th.inputValue = "";
    },
    onPrint(obj) {
      let th = this;
      th.$refs["iframe1"].contentWindow.postMessage(
        obj,
        `${window.location.origin}/print/bf_patientregister.html`
      );
    },
    onFocus() {
      this.inputValue = "";
    },
    onOpen() {
      let th = this;
      th.$nextTick(function() {
        th.$refs["input1"].focus();
      });
    },
    onClose() {
      let th = this;
      th.inputValue = "";
      th.$emit("input", false);
    }
  }
};
</script>
<style lang="scss">
.dialog-input-box {
  > input {
    height: 50px !important;
  }
}
</style>